import {Injectable} from '@angular/core';
import {CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Resolve, Router} from '@angular/router';
import {Observable} from 'rxjs';
import {Product, ProductComponent} from "../components/product/product.component";

/*在组建初始化之前，从服务器获取数据，避免页面加载过程中，插值表达式未获取值而不显示的情况*/
@Injectable({
  providedIn: 'root'
})
export class ProductResolveGuard implements Resolve<Product> {

  constructor(private router: Router){

  }
  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<Product> | Promise<Product> | Product {
    let productId: number = route.params['id'];
    if(productId == 1){
      return new Product(1,"O(∩_∩)O哈哈~");
    }else{
      this.router.navigate(['/home']);
    }
    return undefined;
  }

}
